package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.dubbo.mappers.OmsOrderItemMapper;
import com.mall.dubbo.mappers.OmsOrderOperateHistoryMapper;
import com.mall.dubbo.mappers.OmsOrderReturnApplyMapper;
import com.mall.dubbo.mappers.OrderMapper;
import com.tanhua.model.domain.OmsOrder;
import com.tanhua.model.domain.OmsOrderItem;
import com.tanhua.model.domain.OmsOrderOperateHistory;
import com.tanhua.model.domain.OmsOrderReturnApply;
import com.tanhua.model.dto.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class OrderApiImpl implements OrderApi {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;

    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;

    @Autowired
    private OmsOrderReturnApplyMapper omsOrderReturnApplyMapper;

    /**
     * 查询订单
     *
     * @param orderDto
     * @return
     */
    @Override
    public Page listOmsOrder(OrderDto orderDto) {
        Page<OmsOrder> page = new Page(orderDto.getPageNum(), orderDto.getPageSize());

        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(StringUtils.isNotEmpty(orderDto.getOrderSn()), OmsOrder::getOrderSn, orderDto.getOrderSn())
                .in(StringUtils.isNotEmpty(orderDto.getCreateTime()), OmsOrder::getCreateTime, orderDto.getCreateTime())
                .in(null != orderDto.getOrderType(), OmsOrder::getOrderType, orderDto.getOrderType())
                .in(null != orderDto.getStatus(), OmsOrder::getStatus, orderDto.getStatus())
                .in(null != orderDto.getSourceType(), OmsOrder::getSourceType, orderDto.getSourceType())
                .in(StringUtils.isNotEmpty(orderDto.getReceiverKeyword()), OmsOrder::getReceiverName, orderDto.getReceiverKeyword())
                .or().in(StringUtils.isNotEmpty(orderDto.getReceiverKeyword()), OmsOrder::getBillReceiverPhone, orderDto.getReceiverKeyword());
        Page<OmsOrder> selectPage = orderMapper.selectPage(page, queryWrapper);
        return selectPage;

    }

    /**
     * 获取订单详情：订单信息、商品信息、操作记录
     *
     * @param id
     * @return
     */
    @Override
    public OrderIdAllDto getById(Integer id) {
        if (id == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        OrderIdAllDto dto = new OrderIdAllDto();
        OmsOrder omsOrder = orderMapper.selectById(id);
        BeanUtils.copyProperties(omsOrder, dto);
        LambdaQueryWrapper<OmsOrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OmsOrderItem::getOrderId, id);
        dto.setOrderItemList(omsOrderItemMapper.selectList(queryWrapper));

        LambdaQueryWrapper<OmsOrderOperateHistory> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(OmsOrderOperateHistory::getOrderId, id);
        dto.setHistoryList(omsOrderOperateHistoryMapper.selectList(queryWrapper1));
        return dto;
    }

    /*   *//**
     * 批量发货
     *
     * @param omsOrder
     * @return
     *//*
    @Override
    public boolean upodates(OmsOrder omsOrder) {
        if (omsOrder == null) {
            return false;
        }
        orderMapper.updateById(omsOrder);
        return true;
    }*/

    /**
     * 备注订单
     *
     * @param id
     * @param note
     * @param status
     * @return
     */
    @Override
    public boolean updateNote(Integer id, String note, Integer status) {
        if (id == null) {
            return false;
        }
        OmsOrder omsOrder = orderMapper.selectById(id);
        if (omsOrder == null) {
            return false;
        }
        omsOrder.setStatus(status);
        omsOrder.setNote(note);
        orderMapper.updateById(omsOrder);
        return true;
    }

    /**
     * 修改收货人信息
     *
     * @param dto
     * @return
     */
    @Override
    public boolean updateReceiverInfo(ReceiverDto dto) {
        if (dto == null) {
            return false;
        }
        OmsOrder omsOrder = orderMapper.selectById(dto.getOrderId());
        omsOrder.setModifyTime(new Date(System.currentTimeMillis()));
        BeanUtils.copyProperties(dto, omsOrder);
        orderMapper.updateById(omsOrder);
        //修改oms_order_operate_history表
        LambdaQueryWrapper<OmsOrderOperateHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OmsOrderOperateHistory::getOrderId, omsOrder.getId());
        if (omsOrderOperateHistoryMapper.selectCount(wrapper) > 0) {
            List<OmsOrderOperateHistory> omsOrderOperateHistories = omsOrderOperateHistoryMapper.selectList(wrapper);
            for (OmsOrderOperateHistory omsOrderOperateHistory : omsOrderOperateHistories) {
                if (omsOrderOperateHistory != null) {
                    BeanUtils.copyProperties(dto, omsOrderOperateHistory);
                    omsOrderOperateHistoryMapper.updateById(omsOrderOperateHistory);
                }
            }
        }

        //修改oms_order_return_apply表
        LambdaQueryWrapper<OmsOrderReturnApply> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(OmsOrderReturnApply::getOrderId, omsOrder.getId());
        if (omsOrderReturnApplyMapper.selectCount(wrapper1) > 0) {
            List<OmsOrderReturnApply> applyList = omsOrderReturnApplyMapper.selectList(wrapper1);
            for (OmsOrderReturnApply apply : applyList) {
                if (apply != null) {
                    BeanUtils.copyProperties(dto, apply);
                    omsOrderReturnApplyMapper.updateById(apply);
                }
            }
        }
        return true;
    }

    /**
     * 批量发货
     *
     * @param dto
     * @return
     */
    @Override
    public boolean updateDeliveryo(UpdatedeliveryDto[] dto) {
        if (dto == null) {
            return false;
        }
        for (UpdatedeliveryDto updatedeliveryDto : dto) {
            OmsOrder omsOrder = orderMapper.selectById(updatedeliveryDto.getOrderId());
            if (omsOrder != null) {
                BeanUtils.copyProperties(updatedeliveryDto, omsOrder);
                omsOrder.setDeliveryTime(new Date(System.currentTimeMillis()));
            }

        }
        return true;
    }

    /**
     * 批量删除订单
     *
     * @param ids
     * @return
     */
    @Override
    public boolean deletesw(Integer[] ids) {
        if (ids == null) {
            return false;
        }
        for (Integer id : ids) {
            OmsOrder omsOrder = orderMapper.selectById(id);
            if (omsOrder.getStatus() == 2 || omsOrder.getStatus() == 1) {
                throw new BusinessException(ErrorResult.nullError());
            }
        }

        for (Integer id : ids) {
            LambdaQueryWrapper<OmsOrderReturnApply> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(OmsOrderReturnApply::getOrderId, id);
            List<OmsOrderReturnApply> list = omsOrderReturnApplyMapper.selectList(wrapper);
            //遍历数据
            for (OmsOrderReturnApply apply : list) {
                //如果状态不是2就不给删除
                if (apply.getStatus() != 2) {
                    throw new BusinessException(ErrorResult.nullError());
                }
                omsOrderReturnApplyMapper.deleteById(apply);
            }

            orderMapper.deleteById(id);
        }
        return true;
    }
}
